package com.ntes.controller;

import com.alibaba.fastjson.JSON;
import com.ntes.model.User;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by bjliuyue1 on 2017/5/19.
 */
@RestController
public class DemoController1 {

    @Value("${server.port}")
    int port;
    private boolean flag = true;

    @RequestMapping("/getString")
    public String getString(HttpServletRequest request) {
        String str = "provider1 at port " + port + ":" + request.getParameter("name") + " " + new Date();
        return JSON.toJSONString(str);
    }

    @RequestMapping("/getUser")
    public User getUser(HttpServletRequest request) {
        User user = new User();
        user.setAge(18);
        user.setName(request.getParameter("name"));
        user.setPort(port);
        flag = !flag;
        if (flag) {
            try {
                Thread.currentThread().sleep(1000);
                System.out.println("sleep 1000 ms");
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    @RequestMapping("/getUserString")
    public String getUserString(HttpServletRequest request) {
        User user = new User();
        user.setAge(19);
        user.setName(request.getParameter("name"));
        user.setPort(port);
        return JSON.toJSONString(user);
    }
}
